package com.bong.ring.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.bong.ring.R;

/**
 * TODO: document your custom view class.
 */
public class MyView extends View {
    private float bentPercent = 0.8f;
    private int bentColor = 0xFFFFFFFF;
    private Paint paint;
    private Path path;

    public MyView(Context context) {
        super(context);
        init(null, 0);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.MyView, defStyle, 0);

//        mExampleString = a.getString(
//                R.styleable.MyView_exampleString);
//        mExampleColor = a.getColor(
//                R.styleable.MyView_exampleColor,
//                mExampleColor);
//        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
//        // values that should fall on pixel boundaries.
//        mExampleDimension = a.getDimension(
//                R.styleable.MyView_exampleDimension,
//                mExampleDimension);
//
//        if (a.hasValue(R.styleable.MyView_exampleDrawable)) {
//            mExampleDrawable = a.getDrawable(
//                    R.styleable.MyView_exampleDrawable);
//            mExampleDrawable.setCallback(this);
//        }

        bentPercent = a.getFloat(
                R.styleable.MyView_bentPercent, 0.8f);
        bentColor = a.getColor(R.styleable.MyView_bentColor, 0xFFFFFFFF);
        a.recycle();

        // Set up a default TextPaint object
//        mTextPaint = new TextPaint();
//        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
//        mTextPaint.setTextAlign(Paint.Align.LEFT);

        // Update TextPaint and text measurements from attributes
        invalidateTextPaintAndMeasurements();
    }

    private void invalidateTextPaintAndMeasurements() {
//        mTextPaint.setTextSize(mExampleDimension);
//        mTextPaint.setColor(mExampleColor);
//        mTextWidth = mTextPaint.measureText(mExampleString);
//
//        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
//        mTextHeight = fontMetrics.bottom;
        paint = new Paint();
        paint.setDither(true);
        paint.setColor(bentColor);
        paint.setStyle(Paint.Style.FILL);
        //canvas.drawOval(0,0,contentWidth,contentHeight/2,paint);
        path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;

        path.reset();
        path.moveTo(contentWidth, 0);
        //path.arcTo(0,-contentHeight/2,contentWidth,contentHeight-contentHeight/2,0,180,false);
        path.quadTo(contentWidth / 2, contentHeight * 2 * bentPercent, 0, 0);
        path.lineTo(0, contentHeight);
        path.lineTo(contentWidth, contentHeight);
        path.close();
        canvas.drawPath(path, paint);
//        // Draw the text.
//        canvas.drawText(mExampleString,
//                paddingLeft + (contentWidth - mTextWidth) / 2,
//                paddingTop + (contentHeight + mTextHeight) / 2,
//                mTextPaint);
//
//        // Draw the example drawable on top of the text.
//        if (mExampleDrawable != null) {
//            mExampleDrawable.setBounds(paddingLeft, paddingTop,
//                    paddingLeft + contentWidth, paddingTop + contentHeight);
//            mExampleDrawable.draw(canvas);
//        }
        if (getLayoutParams().height > min && isBack) {
            int offset = (max - min) / 10;
            if (offset < 1)
                offset = 1;
            if (getLayoutParams().height - offset < min)
                getLayoutParams().height = min;
            else
                getLayoutParams().height -= offset;
            post(scrollRunnable);
        }
    }

    public float getBentPercent() {
        return bentPercent;
    }

    public void setBentPercent(float bentPercent) {
        this.bentPercent = bentPercent;
        invalidate();
    }

    Handler handler = new Handler();
    float dy = 0;
    private ProgressView progressView;
    Runnable scrollRunnable = new Runnable() {
        @Override
        public void run() {
            setLayoutParams(getLayoutParams());
            if (progressView != null)
                progressView.setPercent(((float) getLayoutParams().height - min) / (max - min)*1.5f);
        }
    };
    int max = 300, min = 100;

    public float scroll(int dy, int[] consumed) {
        ViewGroup.LayoutParams params = getLayoutParams();
        if (dy < 0 && params.height < max) {//下滑
            if (params.height + (-dy) > max) {
                consumed[1] = params.height - max;
                params.height = max;
            } else {
                params.height += (-dy) / 3;
                consumed[1] = dy;
            }
        } else if (dy > 0 && params.height > min) {//上滑
            if (params.height + (-dy) < min) {
                consumed[1] = params.height - min;
                params.height = min;
            } else {
                params.height += (-dy) / 3;
                consumed[1] = dy;
            }
        } else
            return 0;
        scrollRunnable.run();
        return ((float) params.height - min) / (max - min);
    }

    boolean isBack = true;

    public void isBack(boolean isBack) {
        this.isBack = isBack;
        invalidate();
    }

    public void setProgressView(ProgressView progressView) {
        this.progressView = progressView;
    }
}
